import 'dart:convert';

import 'package:example_example/bean/ToTaskBean.dart';
import 'package:example_example/global/Global.dart';
import 'package:example_example/global/GlobalUrl.dart';
import 'package:example_example/services/EventBus.dart';
import 'package:example_example/util/HttpUtil.dart';
import 'package:example_example/util/LogUtil.dart';
import 'package:example_example/util/Storage.dart';
import 'package:flutter/material.dart';

/**
 * 处理待办任务前先访问接口
 *
 *
 *   flag:
 *   2: 售前立项
 *   3：正式立项
 *   4：PO
 *
 */
setToDoNet(context,bean,flag) async {
  //

  String taskDefinitionKey = bean["taskDefinitionKey"];
  var itemBean = bean;
  if (taskDefinitionKey == null || taskDefinitionKey == "") {
    return;
  }
  String apiUrlhttp = GlobalUrl.UPUSERTASKCONFIG;

  var map = {
    "code": taskDefinitionKey,
  };

  String access_token = await Storage.getToken();
  LogUtil.init(title: "来自LogUtil", isDebug: false, limitLength: 800);
//    LogUtil.v("获取的access_token：$access_token");

  var mapHeaders = {
    Global.AUTHORIZATION: access_token,
    Global.CONTENT_TYPE: Global.APPLICATION_JSON
  };

  HttpUtil.get(apiUrlhttp, data: map, headers: mapHeaders, success: (data) {
    Navigator.of(context).pop();

//      接口的数据：{code: 200, success: true, data: {
//      id: 1400018369129914369,
//      utcTitle: 售前立项一级审核,
//      utcType: 1,
//      utcRoutePath: project/prepare/approve,
//      utcFormType: 1,
//      utcFormId: null,
//      utcInterfaceUrl: /us-
//      process/task/auditOne},
//      msg: 操作成功}

    String str = json.encode(data);
    LogUtil.v("接口的数据22:$str");

    var toTaskBean = ToTaskBean.fromJson(data);

    var success = toTaskBean.success;
    if (success != null && success) {
      print("...是否进来了呀");

      var taskBean = toTaskBean.data;
//
      String utcFormId = taskBean.utcFormId;
      String utcFormType = taskBean.utcFormType;
      String utcInterfaceUrl = taskBean.utcInterfaceUrl;
      String utcRoutePath = taskBean.utcRoutePath;
      String utcTitle = taskBean.utcTitle;
      String utcType = taskBean.utcType;

      if (utcFormId == null) {
        utcFormId = "";
      }
      if (utcFormType == null) {
        utcFormType = "";
      }
      if (utcInterfaceUrl == null) {
        utcInterfaceUrl = "";
      }
      if (utcRoutePath == null) {
        utcRoutePath = "";
      }
      if (utcTitle == null) {
        utcTitle = "";
      }
      if (utcType == null) {
        utcType = "";
      }

      utcTitle = Uri.encodeComponent(utcTitle);

      print("utcFormId:$utcFormId");
      print("utcFormType:$utcFormType");

      // 注意拼接的参数不能有null，否则会报错
//        String url = GlobalUrl.WEB_TODOTASK_URL + "?utcFormType="+utcFormType;

//        print("...拼接的url:"+url);
      String variableJsonStr = json.encode(itemBean["variableJson"]);

      String url = "";
      if (utcFormType == "1") {
        url = GlobalUrl.WEB_STATIC_URL;
      } else if(utcFormType == "2"){
        url = GlobalUrl.WEB_DYNAMIC_URL;
      } else {
        url = GlobalUrl.WEB_CUSTOM_URL;
      }

//        var pathURL ="?utcFormId="+utcFormId+"&utcFormType="+utcFormType+"&platform=app"+"&utcInterfaceUrl="+utcInterfaceUrl
//            +"&utcRoutePath="+utcRoutePath+"&utcTitle="+utcTitle+"&utcType="+utcType+"&name="+utcTitle+"&id="+itemBean["id"]+
//            "&taskDefinitionKey="+taskDefinitionKey+"&processInstanceId="
//            +itemBean["processInstanceId"];

      url = url +
          "?utcFormId=" +
          utcFormId +
          "&utcFormType=" +
          utcFormType +
          "&platform=app" +
          "&utcInterfaceUrl=" +
          utcInterfaceUrl +
          "&utcRoutePath=" +
          utcRoutePath +
          "&utcTitle=" +
          utcTitle +
          "&utcType=" +
          utcType +
          "&name=" +
          utcTitle +
          "&id=" +
          itemBean["id"] +
          "&taskDefinitionKey=" +
          taskDefinitionKey +
          "&processInstanceId=" +
          itemBean["processInstanceId"];

//        url = url+"?"+Uri.encodeComponent(pathUrl);

//       url = url + json.encode(pathURL);
//          url = json.encode(url+pathURL);
//           +"&variableJson="+variableJsonStr;

      Navigator.pushNamed(context, '/webViewUtilPage', //WebViewExample
          arguments: {
            Global.WEBVIEW_TAG: Global.HIISTORYTASK,
            Global.WEBVIEW_URL: url,

            "id": bean["id"],
//                            "count":count++,
            'variableJsonStr': variableJsonStr,
          }).then((value) {
        print('webviewz这个有吗:$value');
        if(value!=null&&value=="refresh"){
          print('去刷新了$flag');

          /**
           * 发送广播
           */
          eventBus.fire(new ReFresshEvent(flag));

//          if(flag == "2"){
//          }else if(flag == "3"){
//          }else if(flag == "4"){
//          }

        }

      });
    } else {}
  }, error: (errorMsg) {
    print("//....error了啊");
    Navigator.of(context).pop();
  });
}